package com.crm.system.mapper;

import com.crm.dto.DictDTO;
import com.crm.entity.SysDict;
import com.crm.entity.SysRegion;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysDictMapper {
    @Select("select * from crm_sys_dict where disabled=0  order by group_name, dic_value")
    List<SysDict> findAll();

    @Select("SELECT DISTINCT (group_key) from crm_sys_dict")
    List<String> listMap();

    @Select("select * from crm_sys_dict where disabled=0 and group_name=#{group} order by dic_value")
    List<SysDict> findByGroup(String group);

    @Update("UPDATE crm_sys_dict\n" +
            "SET group_name=#{groupName}, dic_key=#{dicKey}, " +
            "dic_value=#{dicValue}\n" +
            "WHERE id=#{id}")
    int update(SysDict dict);

    @Update("UPDATE crm_sys_dict\n" +
            "SET disabled=1 " +
            "WHERE id=#{id}")
    int delete(int id);

    @Select("select * from crm_sys_dict where id=#{id}")
    SysDict get(int id);

    @Select("select distinct(group_name),group_key from crm_sys_dict")
    List<DictDTO> findGroup();


    @Insert("INSERT INTO crm_sys_dict\n" +
            "(group_name, dic_key, dic_value, disabled,group_key)\n" +
            "VALUES(#{groupName}, #{dicKey}, #{dicValue}, 0,#{groupKey});")
    int add(SysDict sysDict);

    @Select("SELECT group_key FROM crm_sys_dict where group_name =#{groupName} LIMIT 1")
    String groupKey(String groupName);

    @Select("select * from sys_region ")
    List<SysRegion> listCity();
}
